package com.mobisys.live.controller.mobile_web;

import com.mobisys.live.bean.channel.GetAllLiveList;
import com.mobisys.live.bean.live.Guest;
import com.mobisys.live.bean.live.Schedule;
import com.mobisys.live.controller.LiveController;
import com.mobisys.live.dao.mapper.GroupInfoMapper;
import com.mobisys.live.dao.mapper.LiveMapper;
import com.mobisys.live.dao.mapper.VideoMapper;
import com.mobisys.live.dao.model.LiveWithBLOBs;
import com.mobisys.live.dao.model.Video;
import com.mobisys.live.response.Response;
import com.mobisys.live.response.channel.LiveResponseBody;
import com.mobisys.live.response.channel.VideoResponseBody;
import com.mobisys.live.response.live.LiveInfoResponseBody;
import com.mobisys.live.util.IdStrUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

import java.awt.*;
import java.beans.IntrospectionException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import static com.mobisys.live.config.ResponseCode.*;

/**
 * @author gwg
 * @date 2018/3/28 17:02
 */
@RestController
public class LivePlayController {
    private Logger logger= LogManager.getLogger(LivePlayController.class.getName());

    @Autowired
    private LiveMapper liveMapper;

    @Autowired
    private VideoMapper videoMapper;

    @RequestMapping(value = "/mobile/livePlay",method = RequestMethod.POST)
    public Response getLive(@RequestBody GetAllLiveList getAllLiveList, BindingResult bindingResult, HttpServletRequest httpServletRequest){
        Response response = new Response<LiveInfoResponseBody>();

        //检查输入是否合法
        //检查输入的合法性
        if(bindingResult.hasErrors()){
            response.setRetureValue(INPUT_IS_INVALID);
            response.setRetureMsg(MSG_INPUT_IS_INVALID);

            return response;
        }
        List<LiveWithBLOBs> liveList = liveMapper.selectLiveListByChannelId(getAllLiveList.getChannelId());
        if(liveList==null) {
            response.setRetureValue(DATABASE_ERROR);
            response.setRetureMsg(MSG_DATABASE_ERROR);
            return response;
        }
        LiveInfoResponseBody liveResponseBody = new LiveInfoResponseBody();
        int num = 0;
        for(LiveWithBLOBs live:liveList){
            if(live.getLiveStatus()==1){
                num++;
                liveResponseBody.setUserId(live.getUserId());
                liveResponseBody.setChannelId(live.getChannelid());
                liveResponseBody.setGroupId(live.getGroupid());
                liveResponseBody.setPushUrl(live.getPushUrl());

                //直播观看地址
                liveResponseBody.setHlsPlayUrl(live.getHlsPlayUrl());
                liveResponseBody.setFlvPlayUrl(live.getFlvPlayUrl());
                liveResponseBody.setRtmpPlayUrl(live.getRtmpPlayUrl());

                liveResponseBody.setTitle(live.getTitle());
                liveResponseBody.setLocation(live.getLocation());
                liveResponseBody.setLikeCount(live.getLikeCount());
                liveResponseBody.setLiveStatus(live.getLiveStatus());
                liveResponseBody.setViewCount(live.getViewCount());
                liveResponseBody.setType(live.getType());
                liveResponseBody.setIntroduction(live.getIntroduction());

                liveResponseBody.setStartTime(live.getCreateTime());
                liveResponseBody.setEndTime(live.getEndTime());
//        //获取嘉宾
//        List<Integer> guestListId = IdStrUtil.convertToIdList(live.getGuest());
//
//        //获取议程
//        List<Integer> scheduleListId=IdStrUtil.convertToIdList(live.getSchduled());
            }
        }
        if(num==0){
            response.setRetureValue(NOT_IN_LIVE);
            response.setRetureMsg(MSG_NOT_IN_LIVE);
            return response;
        }
        if(num>1){
            response.setRetureValue(MORE_THAN_ONE_LIVE_IN_CHANNEL);
            response.setRetureMsg(MSG_MORE_THAN_ONE_LIVE_IN_CHANNEL);
            return response;
        }
        response.setRetureValue(SUCCESS);
        response.setRetureMsg(MSG_SUCCESS);
        response.setRetureData(liveResponseBody);
        return response;
    }
    @RequestMapping(value ="/mobile/getVideoInChannel",method = RequestMethod.POST)
    public Response getChannelVideo(@RequestBody GetAllLiveList getAllLiveList, BindingResult bindingResult, HttpServletRequest httpServletRequest){

        Response response = new Response<List<VideoResponseBody>>();
        //检查输入是否合法
        //检查输入的合法性
        if(bindingResult.hasErrors()){
            response.setRetureValue(INPUT_IS_INVALID);
            response.setRetureMsg(MSG_INPUT_IS_INVALID);

            return response;
        }
        List<Video> videoList = videoMapper.selectByChannelId(getAllLiveList.getChannelId());
        if(videoList==null) {
            response.setRetureValue(DATABASE_ERROR);
            response.setRetureMsg(MSG_DATABASE_ERROR);
            return response;
        }
        List<VideoResponseBody> responseBodyList=new ArrayList<>();
        for (Video list: videoList){
            VideoResponseBody videoResponseBody = new VideoResponseBody();
            videoResponseBody.setTitle(list.getTitle());
            videoResponseBody.setPlayUrl(list.getPlayUrl());
            videoResponseBody.setCoverPictureId(list.getCoverPictureId());
            videoResponseBody.setIntroduction(list.getIntroduction());
            videoResponseBody.setCreateTime(list.getCreateTime());
            videoResponseBody.setLikeCount(list.getLikeCount());
            videoResponseBody.setViewCount(list.getViewCount());
            videoResponseBody.setType(list.getType());
            responseBodyList.add(videoResponseBody);
        }
        response.setRetureValue(SUCCESS);
        response.setRetureMsg(MSG_SUCCESS);
        response.setRetureData(responseBodyList);
        return response;
    }

}
